package cn.iocoder.yudao.module.carbon.controller.admin.accountboundary;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.carbon.controller.admin.accountboundary.vo.*;
import cn.iocoder.yudao.module.carbon.convert.accountboundary.AccountBoundaryConvert;
import cn.iocoder.yudao.module.carbon.dal.dataobject.accountboundary.AccountBoundaryDO;
import cn.iocoder.yudao.module.carbon.service.accountboundary.AccountBoundaryService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name =  "管理后台 - 核算边界")
@RestController
@RequestMapping("/carbon/account-boundary")
@Validated
public class AccountBoundaryController {

    @Resource
    private AccountBoundaryService accountBoundaryService;

    @PostMapping("/create")
    @Operation(summary = "创建核算边界")
    @PreAuthorize("@ss.hasPermission('carbon:account-boundary:create')")
    public CommonResult<Long> createAccountBoundary(@Valid @RequestBody AccountBoundaryCreateReqVO createReqVO) {
        return success(accountBoundaryService.createAccountBoundary(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新核算边界")
    @PreAuthorize("@ss.hasPermission('carbon:account-boundary:update')")
    public CommonResult<Boolean> updateAccountBoundary(@Valid @RequestBody AccountBoundaryUpdateReqVO updateReqVO) {
        accountBoundaryService.updateAccountBoundary(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除核算边界")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('carbon:account-boundary:delete')")
    public CommonResult<Boolean> deleteAccountBoundary(@RequestParam("id") Long id) {
        accountBoundaryService.deleteAccountBoundary(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得核算边界")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('carbon:account-boundary:query')")
    public CommonResult<AccountBoundaryRespVO> getAccountBoundary(@RequestParam("id") Long id) {
        AccountBoundaryDO accountBoundary = accountBoundaryService.getAccountBoundary(id);
        return success(AccountBoundaryConvert.INSTANCE.convert(accountBoundary));
    }

    @GetMapping("/page")
    @Operation(summary = "获得核算边界分页")
    @PreAuthorize("@ss.hasPermission('carbon:account-boundary:query')")
    public CommonResult<PageResult<AccountBoundaryRespVO>> getAccountBoundaryPage(@Valid AccountBoundaryPageReqVO pageVO) {
        PageResult<AccountBoundaryDO> pageResult = accountBoundaryService.getAccountBoundaryPage(pageVO);
        return success(AccountBoundaryConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出核算边界 Excel")
    @PreAuthorize("@ss.hasPermission('carbon:account-boundary:export')")
    @OperateLog(type = EXPORT)
    public void exportAccountBoundaryExcel(@Valid AccountBoundaryExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<AccountBoundaryDO> list = accountBoundaryService.getAccountBoundaryList(exportReqVO);
        // 导出 Excel
        List<AccountBoundaryExcelVO> datas = AccountBoundaryConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "核算边界.xls", "数据", AccountBoundaryExcelVO.class, datas);
    }

}
